package client.comm.baoding.adapter

import android.content.Context
import android.content.Intent
import android.os.Parcelable
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.core.content.ContentProviderCompat.requireContext
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.MutableLiveData
import androidx.paging.LoadState
import androidx.paging.LoadStateAdapter
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import client.comm.baoding.R
import client.comm.baoding.api.bean.GoodsIndex
import client.comm.baoding.api.bean.UserInfo
import client.comm.baoding.databinding.*
import client.comm.baoding.ui.TabOne2Activity
import client.comm.baoding.widget.HorizontalPageLayoutManager
import client.comm.baoding.widget.PagingScrollHelper
import client.comm.commlib.base.PagingViewHoder
import client.comm.commlib.base.SyBannerAdapter
import com.bumptech.glide.RequestManager
import com.bumptech.glide.request.RequestOptions
import com.youth.banner.indicator.CircleIndicator
import client.comm.commlib.getScreenWidth
import client.comm.commlib.utils.StoreUtil
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener

class FejPagingHeaderAdapter(val context: Context) :
    LoadStateAdapter<PagingViewHoder>() {

    val guadanJe: MutableLiveData<String> = MutableLiveData()

    override fun onCreateViewHolder(parent: ViewGroup, loadState: LoadState): PagingViewHoder {
        return PagingViewHoder(
            DataBindingUtil.inflate<ViewDataBinding?>(
                LayoutInflater.from(parent.context),
                R.layout.pagingitem_fejheader,
                parent,
                false
            ).apply {

                this as PagingitemFejheaderBinding

                tabLayout.addOnTabSelectedListener(object : OnTabSelectedListener {
                    override fun onTabSelected(tab: TabLayout.Tab?) {
                        tab?.let {
                            onChange?.invoke(if (it.position == 0) "11" else "12")
                        }
                    }

                    override fun onTabUnselected(tab: TabLayout.Tab?) {
                    }

                    override fun onTabReselected(tab: TabLayout.Tab?) {
                    }

                })
            }
        )
    }

    override fun onBindViewHolder(holder: PagingViewHoder, loadState: LoadState) {
        StoreUtil.getBean<UserInfo>(context)?.let {
            val binding = holder.binding as PagingitemFejheaderBinding

            binding.curPrice = it.stock_price
            binding.clockFej = "${it.integral_f}"
            binding.kyFej = "${it.integral_fx}"

            binding.event = this
        }
    }

    var hzCli: ((String?) -> Unit)? = null

    var onChange: ((String) -> Unit)? = null

    fun hzClick() {
        hzCli?.invoke(guadanJe.value)
    }

    override fun onViewAttachedToWindow(holder: PagingViewHoder) {
        super.onViewAttachedToWindow(holder)
        val lp: ViewGroup.LayoutParams = holder.itemView.getLayoutParams()
        if (lp != null && lp is StaggeredGridLayoutManager.LayoutParams
        ) {
            lp.isFullSpan = true
        }
    }

    // true显示无论什么状态都显示
    override fun displayLoadStateAsItem(loadState: LoadState): Boolean {
        return super.displayLoadStateAsItem(loadState) || (loadState.endOfPaginationReached)
    }


}